﻿using Max.PH5_2006.ShoppingSite.Domain;
using Max.PH5_2006.ShoppingSite.Domain.Shared;
using Max.PH5_2006.ShoppingSite.DTO;
using Max.PH5_2006.ShoppingSite.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2006.ShoppingSite.API.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [Route("order")]
    [ApiController]
    public class OrderController : ControllerBase
    {

        IOrderService _service;
        /// <summary>
        /// 订单控制器
        /// </summary>
        public OrderController(IOrderService service) 
        {
            _service = service;
        }

        #region 订单的基本操作

        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="memberId"></param>
        /// <param name="orderStatue"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Query(string memberId="",string? orderStatue="",int pageIndex=1,int pageSize = 10)
        {
            ResponseResultDto response = new();
            
            var data = _service.Query().Where(x=>x.MemberId == new Guid(memberId));
            if(Convert.ToInt32(orderStatue) >= 0)  //查全部的，我传的是-1，所以用判断>=0来作为条件
                data = data.Where(x=>x.OrderStatue==  (OrderStatueEnum)Convert.ToInt32(orderStatue));  //状态是枚举类型，所以将字符串，先强转为整形，再转为枚举
            response.Data = data;

            return Ok(response);

        }

        #endregion

        #region 订单业务


        /// <summary>
        /// 生成订单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        //添加订单--生成订单
        [HttpPost("add")]
        public IActionResult Post(SettleOrderDto dto)
        {
            ResponseResultDto response = new();

            OrderEntity order = dto.Order; //new();
            List<OrderDetailEntity> orderDetail = dto.OrderDetail; //new();

            var result = _service.OrderAdd(order, orderDetail);

            //if (!result.Item1)
            //{
            //    response.Message = result.Item2;
            //}

            response.Message = result.Item2;
            response.Data = result.Item3; //订单号

            return Ok(response);
        }

        /// <summary>
        /// 订单支付
        /// </summary>
        /// <param name="orderNo"></param>
        /// <param name="payType"></param>
        /// <returns></returns>
        //订单支付
        [HttpGet("pay")]
        public IActionResult GetPay(string orderNo,int payType= 0)
        {
            /*
             * 立即支付按钮，需要做三件事
             * 1、调用支付接口进行支付
             * 2、修改订单状态
             * 3、修改商品库存
             * 
             * **/

            ResponseResultDto response = new();

            var result = _service.OrderPay(orderNo, (PayTypeEnum)payType);

            response.Message= result.Item2;

            return Ok(response);
        }

        #endregion

        /// <summary>
        /// 测试订单支付
        /// </summary>
        /// <returns></returns>
        [HttpGet("pay/test")]
        public IActionResult PostPayTest()
        {
            ResponseResultDto response = new();

            Common.Pay.IPay pay = new Common.Pay.AlipayPay();
            var result = pay.Pay();

            response.Message = result.Item2;

            return Ok(response);
        }

    }


}
